package noidungvanban.backing;

import java.util.TimeZone;

import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import noidungvanban.noidungvanban;

import noidungvanban.resource.noidungvanbanBundle;

public class noidungvanbanBacking {
    private String mPortletTitle;
    private String Urlchitietvanban;
    private String UrlDsvanbangopy;
    private String tieudendvanban;

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(noidungvanban.PORTLETTITLE_KEY, getResource(noidungvanbanBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(noidungvanbanBundle.PORTLETTITLE);
        return resource;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(noidungvanban.PORTLETTITLE_KEY, mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(noidungvanbanBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response = (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(noidungvanbanBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        PortletRequest request = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletConfig config = (PortletConfig)request.getAttribute("javax.portlet.config");
        return config.getResourceBundle(request.getLocale()).getString(resourceKey);
    }
    
    private String UrlChitietvanban() {
        String urlChitiet = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("Urlchitietvb") != null)
                urlChitiet = request.getParameter("Urlchitietvb").toString();
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return urlChitiet;
    }
    
    private String TieudeNoidungvanban() {
        String tieudeNdvanban = "Danh sách văn bản";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieudendvanban") != null)
                tieudeNdvanban = request.getParameter("tieudendvanban").toString();
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return tieudeNdvanban;
    }
    
    private String UrlDsvanban() {
        String urlDsvanban = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("Urldsvbgopy") != null)
                urlDsvanban = request.getParameter("Urldsvbgopy").toString();
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return urlDsvanban;
    }
    
    public TimeZone getTimeZone(){
        return TimeZone.getTimeZone("GMT+7");
    }

    public void setUrlchitietvanban(String Urlchitietvanban) {
        this.Urlchitietvanban = Urlchitietvanban;
    }

    public String getUrlchitietvanban() {
        Urlchitietvanban = UrlChitietvanban();
        return Urlchitietvanban;
    }

    public void setUrlDsvanbangopy(String UrlDsvanbangopy) {
        this.UrlDsvanbangopy = UrlDsvanbangopy;
    }

    public String getUrlDsvanbangopy() {
        UrlDsvanbangopy = UrlDsvanban();
        return UrlDsvanbangopy;
    }

    public void setTieudendvanban(String tieudendvanban) {
        this.tieudendvanban = tieudendvanban;
    }

    public String getTieudendvanban() {
        tieudendvanban = TieudeNoidungvanban();
        return tieudendvanban;
    }
}
